package com.coder520.sync_learning.demo;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.coder520.sync_learning.demo.CyclicBarrierTest2.A;

public class ExchangerTest {
	private static final Exchanger< String> EXCHANGER=new Exchanger<String>();
	private static ExecutorService threadPool=Executors.newFixedThreadPool(2);
	public static void main(String[] args) {
		threadPool.execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					String A="银行流水A";
					EXCHANGER.exchange(A);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		
		threadPool.execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					String B="银行流水B";
					
					String A=EXCHANGER.exchange("");
					System.out.println("AB数据是否一致"+A.equals(B)+"\nA录入的是:"+A+"\nB录入的是:"+B);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		threadPool.shutdown();
	}
}
